package com.example.bili.view.fragment;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.example.bili.R;
import com.example.bili.adapter.WHisplayPageAdapter10;
import com.example.bili.adapter.WHisplayPageAdapterx;
import com.example.bili.allbean.HisplaypageBean;
import com.example.bili.allbean.HisplaypageBean2;
import com.example.bili.base.BaseFragment;
import com.example.bili.module.OkHttp;
import com.example.bili.util.GlideImageLoader;
import com.example.bili.view.activity.WHisplaypageXActivity;
import com.google.gson.Gson;
import com.youth.banner.Banner;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Request;

import static com.example.bili.internet.connectors.BANGUMI_BASE_URL;
import static com.example.bili.internet.connectors.HISPLAYPAGE_URL;

/**
 * Created by Administrator on 2017/3/8. 番剧
 */

public class hisplaypage extends BaseFragment implements WHisplayPageAdapterx.OnRVitemClickListener {
    private List<String> stringList = new ArrayList<>();
    private Banner banner;
    private RecyclerView w_hisplaypage_recycleView_x;
    private RecyclerView w_hisplaypage_recycleView_10;
    private RecyclerView w_hisplaypage_recycleView_f;
    private List<HisplaypageBean.ResultBean.AdBean.HeadBean> head;
    private List<HisplaypageBean.ResultBean.SerializingBean> serializing;
    private List<HisplaypageBean.ResultBean.PreviousBean.ListBean> list;
    private WHisplayPageAdapterx r1Adapter;
    private SwipeRefreshLayout refreshLayout;

    @Override
    protected void initview(View view) {
        banner = (Banner) view.findViewById(R.id.banner);
        w_hisplaypage_recycleView_x = (RecyclerView) view.findViewById(R.id.w_hisplaypage_recycleView_x);
        w_hisplaypage_recycleView_10 = (RecyclerView) view.findViewById(R.id.w_hisplaypage_recycleView_10);
        w_hisplaypage_recycleView_f = (RecyclerView) view.findViewById(R.id.w_hisplaypage_recycleView_f);
        refreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refresh_layout);
    }

    @Override
    protected void m_onCreateView() {
        getDataFromNet();
        getDataFromNet2();
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //请求数据
                if (stringList != null) {
                    stringList.clear();
                }
                getDataFromNet();
                getDataFromNet2();

            }
        });
    }
    private void getDataFromNet2(){
        HashMap<String, String> map = new HashMap<>();
        map.put("access_key", "f5bd4e793b82fba5aaf5b91fb549910a");
        map.put("actionKey", "appkey");
        map.put("appkey", "27eb53fc9058f8c3");
        map.put("build", "3470");
        map.put("cursor", "0");
        map.put("device", "phone");
        map.put("mobi_app", "iphone");
        map.put("pagesize", "10");
        map.put("platform", "ios");
        map.put("sign", "56329a5709c401d4d7c0237f64f7943f");
        map.put("ts", "1469613558");


        OkHttp.postAsync(BANGUMI_BASE_URL, map, new OkHttp.DataCallBack() {
            @Override
            public void requestFailure(Request request, IOException e) {

            }

            @Override
            public void requestSuccess(String result) throws Exception {

            }


        });
    }
    private void getDataFromNet() {
        refreshLayout.setRefreshing(true);
        HashMap<String, String> map = new HashMap<>();
        map.put("build", "3940");
        map.put("device", "phone");
        map.put("mobi_app", "iphone");
        map.put("platform", "ios");

        OkHttp.postAsync(HISPLAYPAGE_URL, map, new OkHttp.DataCallBack() {
            @Override
            public void requestFailure(Request request, IOException e) {

            }

            @Override
            public void requestSuccess(String result) throws Exception {
                Gson gson = new Gson();
                HisplaypageBean hisplaypageBean = gson.fromJson(result, HisplaypageBean.class);
                head = hisplaypageBean.getResult().getAd().getHead();
                serializing = hisplaypageBean.getResult().getSerializing();
                list = hisplaypageBean.getResult().getPrevious().getList();
                //无线轮播
                getBanner();
                //适配数据
                getRecyclerView();

                refreshLayout.setRefreshing(false);
            }


        });
    }

    //适配数据
    private void getRecyclerView() {
        r1Adapter = new WHisplayPageAdapterx(getContext(), serializing);
        w_hisplaypage_recycleView_x.setLayoutManager(new GridLayoutManager(getContext(), 3));
        w_hisplaypage_recycleView_x.setAdapter(r1Adapter);
        r1Adapter.setOnRVitemClickListener(this);

        WHisplayPageAdapter10 r2Adapter = new WHisplayPageAdapter10(getContext(), list);
        w_hisplaypage_recycleView_10.setLayoutManager(new GridLayoutManager(getContext(), 3));
        w_hisplaypage_recycleView_10.setAdapter(r2Adapter);
        r2Adapter.setOnRVitemClickListener(new WHisplayPageAdapter10.OnRVitemClickListener() {
            @Override
            public void onClickListener(int position) {

            }
        });

    }

    //无线轮播
    public void getBanner() {
        stringList.add(head.get(0).getImg());
        stringList.add(head.get(1).getImg());
        stringList.add(head.get(2).getImg());
        stringList.add(head.get(3).getImg());

        //适配轮播图
        banner.setImageLoader(new GlideImageLoader());
        banner.setImages(stringList);
        banner.start();
    }

    //加载布局
    @Override
    public View getLayoutview() {
        return View.inflate(getActivity(), R.layout.hisplaypage, null);
    }


    @Override
    public void onClickListener(int position) {

        Intent intent = new Intent(getContext(), WHisplaypageXActivity.class);
        getContext().startActivity(intent);
    }


}
